﻿using CleverDD.Helper;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using CleverDD.Bll.Enum;
using HslCommunication;
using CleverDD.Helper.Communication.PLC;

namespace CleverDD.Bll
{
    public class CommonTool
    {
        /// <summary>
        /// 无论是什么设备、什么参数配置，都可以加载
        /// </summary>
        public void SafeLoad()
        {
            LoadAllJsonParam();

            StaticTool.InitSerialPortToolList();
            StaticTool.InitPlcToolList();
        }

        public void LoadAllJsonParam()
        {
            StaticTool.InitStaticSerialPortInfoList();//加载 SerialPortInfoList 集合
            StaticTool.InitStaticAllPlcInfoList();//加载 PlcInfoList、PlcAddressInfoList 集合
            StaticTool.InitStaticAxisInfoList(); //加载 AxisInfoList 集合
        }

        #region PLC

        /// <summary>
        /// 打开所有启用的Plc
        /// </summary>
        public bool OpenAllPlc()
        {
            foreach (var plcTool in StaticTool.PlcToolList)
            {
                if (!plcTool.ConnPlc().IsSuccess)
                {
                    return false;
                }
            }
            return true;
        }

        /// <summary>
        /// 打开某一个Plc
        /// </summary>
        public OperateResult OpenPlc(PlcName plcName)
        {
            OperateResult result = new OperateResult();
            if ((int)plcName >= StaticTool.PlcToolList.Count)
            {
                result.IsSuccess = false;
                result.Message = "PLC工具数组越界";
                return result;
            }

            return StaticTool.PlcToolList[(int)plcName].ConnPlc();

        }

        /// <summary>
        /// 获取Plc信息
        /// </summary>
        /// <param name="plcAddressName"></param>
        /// <param name="plcName"></param>
        /// <returns></returns>
        public OperateResult<string> GetPlcValue(PlcAddressName plcAddressName, PlcName plcName = default)
        {
            OperateResult<string> operateResult = new OperateResult<string>();
            if ((int)plcName >= StaticTool.PlcToolList.Count)
            {
                operateResult.IsSuccess = false;
                operateResult.Message = "PLC信息越界，请查看编辑界面";
                return operateResult;
            }

            if ((int)plcAddressName >= StaticTool.PlcToolList[(int)plcName].CurrentPlcAddressInfo.Count)
            {
                operateResult.IsSuccess = false;
                operateResult.Message = "PLC地址信息越界，请查看编辑界面";
                return operateResult;
            }

            if (StaticTool.PlcToolList[(int)plcName].CurrentPlcAddressInfo[(int)plcAddressName].ReadSuccess)
            {
                operateResult.IsSuccess = false;
                operateResult.Content = StaticTool.PlcToolList[(int)plcName].CurrentPlcAddressInfo[(int)plcAddressName].Value;
                operateResult.Message = "未实际读取成功";
            }
            else
            {
                operateResult.IsSuccess = true;
                operateResult.Content = StaticTool.PlcToolList[(int)plcName].CurrentPlcAddressInfo[(int)plcAddressName].Value;
            }
            return operateResult;


        }


        /// <summary>
        /// 写入Plc信息
        /// </summary>
        /// <param name="value"></param>
        /// <param name="plcAddressName"></param>
        /// <param name="plcName">不传递默认向第一个Plc里面写入</param>
        /// <returns></returns>

        public OperateResult WritePlcValue(PlcAddressName plcAddressName, string value, PlcName plcName = default)
        {
            OperateResult<string> operateResult = new OperateResult<string>();
            if ((int)plcName >= StaticTool.PlcToolList.Count)
            {
                operateResult.IsSuccess = false;
                operateResult.Message = "PLC信息越界，请查看编辑界面";
                return operateResult;
            }

            return StaticTool.PlcToolList[(int)plcName].WritePlcValue((int)plcAddressName, value);

        }


        #endregion


        #region 串口

        /// <summary>
        /// 发送串口数据的方法
        /// </summary>
        /// <param name="serialPortName"></param>
        /// <param name="data">不传就发送默认值</param>
        /// <returns></returns>
        public OperateResult<string> SerialPortSendData(SerialPortName serialPortName, string data = "")
        {
            OperateResult<string> operateResult = new OperateResult<string>();
            if ((int)serialPortName > StaticTool.SerialPortToolList.Count - 1)
            {
                operateResult.IsSuccess = false;
                operateResult.Message = "串口信息越界，请查看编辑界面";
                return operateResult;
            }
            if ("".Equals(data))
            {
                operateResult = StaticTool.SerialPortToolList[(int)serialPortName].SendDefaultData();
            }
            else
            {
                operateResult = StaticTool.SerialPortToolList[(int)serialPortName].SendData(data);
            }
            return operateResult;
        }

        #endregion









        public void CloseAll()
        {

        }

        

    }
}
